package ControlLayer;

import java.sql.SQLException;
import java.util.ArrayList;

import DBLayer.DBConnection;
import DBLayer.DBTreatment;
import DBLayer.IFDBTreatment;
import ExceptionLayer.CustomException;
import ModelLayer.Treatment;
import ModelLayer.TreatmentCategory;

public class TreatmentCtr {
	
	private IFDBTreatment treatDB;
		
	public TreatmentCtr()
	{
		treatDB = new DBTreatment();
	}
		
	public ArrayList<TreatmentCategory> getTreatCategories()
	{
		TreatCatCtr treatCat = new TreatCatCtr();
		
		return treatCat.getTreatCategories();
	}
	
	public ArrayList<Treatment> getTreatmentsByCatId(int catId)
	{
		return treatDB.getTreatmentsByCatId(catId);
	}
	
	public ArrayList<Treatment> getAllTreatmentsBySearchword(String word)
	{
		return treatDB.getAllTreatmentsBySearchword(word);
	}
	
	public Treatment getTreatmentById(int id)
	{
		return treatDB.getTreatmentById(id);
	}
	
	public void updateTreatment(Treatment treatment) throws CustomException
	{
			treatDB.updateTreatment(treatment);
	}
	
	public void newTreatment(Treatment treat) throws CustomException
	{
		
		
		try
		{
			DBConnection.startTransaction();
			
			treat.setType("klipning");
			
			treat.calcTotalTime();
			
			treatDB.insertTreatment(treat);
			
			if(treat.getSubTreatments().size() > 0)
			{
				TreatmentRelCtr relCtr = new TreatmentRelCtr();
				
				for(Treatment subTreat : treat.getSubTreatments())
				{
					relCtr.addTreatmentRel(treat, subTreat);
				}
			}
			
			DBConnection.commitTransaction();
		}
		catch(CustomException ex)
		{
			DBConnection.rollbackTransaction();
			
			throw ex;
		}
	}
	
	public void deleteTreatmentById(int id) throws CustomException, SQLException
	{
		treatDB.deleteTreatmentById(id);
	}
	
	public int getMax()
	{
		return treatDB.getMax();
	}
	
	
}






